#Include ..\Extend_Merged.ahk

class RadioGroup {
  radios := []

  __New(_rect) {
    this._rect := _rect
  }

  __Get(key, Params) => this[key]

  AddRadios(ins*) {
    this.radios.Push(ins*)
  }

  Draw(canvas) {
    this.radios.foreach(v => v.draw(canvas, this._rect.x, this._rect.y))
  }

  Check(i) {
    for idx, v in this.radios
      i = idx ? v.OnChecked() : v.OnUnChecked()
    this.checkedIdx := i
  }

  Press(i) {
    for idx, v in this.radios
      i = idx ? v.OnPressed() : v.OnRelease()
    this.pressedIdx := i
  }

  OnLD(point, *) {
    ; 设置按下状态
    if !this._rect.PointIn(point)
      return
    for v in this.radios {
      if v._rect.PointIn(point, this._rect.x, this._rect.y) {
        this.Press(A_Index)
        return
      }
    }
  }

  OnLU(point, *) {
    ; 设置选中状态
    if !this.pressedIdx
      return
    this.Press(0)
    if !this._rect.PointIn(point)
      return
    for v in this.radios {
      if v._rect.PointIn(point, this._rect.x, this._rect.y) {
        this.Check(A_Index)
        return
      }
    }
  }
}